#include <vector>
#include <queue>
#include "sched_rr.h"
#include "basesched.h"

using namespace std;

SchedRR::SchedRR(vector<int> argn) {
    quantum = argn[0];
}

void SchedRR::load(int pid) {
    quanta.push_back(quantum);
    cola.push(pid);
}

void SchedRR::unblock(int pid) {
	quanta[pid] = quantum;
	cola.push(pid);
}

int SchedRR::tick(const enum Motivo m) {
    if (cola.empty()) return IDLE_TASK;

    int pid = current_pid();
    if (pid == IDLE_TASK) return cola.front();

    if (m == TICK) {
    	quanta[pid]--;
        if (quanta[pid] == 0) {
        	quanta[pid] = quantum;
        	cola.pop();
        	cola.push(pid);
        }
    } else if (m == EXIT || m == BLOCK) {
    	cola.pop();
    }

    return cola.empty() ? IDLE_TASK : cola.front();
}
